package gui;


import Projektarbete.ContactTabListFiller;
import Projektarbete.Group;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

/*
 * Added chat and folder when opening GroupPanel!
 * 26-04-12.
 */

public class GroupPanel extends javax.swing.JPanel {
	private JPanel scrollPanel = new JPanel();
	private ArrayList<GroupBox> groups = new ArrayList<GroupBox>();
	private MainFrame mf;

	public GroupPanel(MainFrame mf) {
		this.mf = mf;
		JScrollPane scrollPane = initComponents();
		scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.Y_AXIS));
		scrollPane.setViewportView(scrollPanel);
		ContactTabListFiller filler = new ContactTabListFiller();
		fillGroupPanel(filler.getContactGroupList(mf.getUser()));


	}

	public void addGroup(Group group) {

		JLabel label = new JLabel(group.getName(),new ImageIcon("src/group.jpeg"),0);

		JPanel outer = new JPanel(new GridLayout(1, 1));
		JPanel inner = new JPanel(new GridLayout(1, 1));

		groups.add(new GroupBox(outer, inner, label, group));

		inner.setBackground(Color.white);
		inner.setBorder(BorderFactory.createLineBorder(Color.black));
		inner.add(label);
		ListenerPanel lp = new ListenerPanel();
		inner.addMouseListener(lp);
		label.addMouseListener(lp);
		setOpaque(false);
		outer.setBorder(new EmptyBorder(3, 3, 3, 3));
		outer.add(inner);
		outer.setPreferredSize(new Dimension(230, 70));
		outer.setMinimumSize(new Dimension(240, 70));
		outer.setMaximumSize(new Dimension(240, 70));

		scrollPanel.add(outer);
		scrollPanel.updateUI();
	}

	public void fillGroupPanel(ArrayList<Group> groups){
		for (int i = 0; i < groups.size(); i++) {
			addGroup(groups.get(i));
		}
	}
	
	public void updateGroup(Group group){
		for(int i = 0; i < groups.size(); i++){
			if(groups.get(i).group.getId() == group.getId()){
				groups.get(i).group = group;
				this.updateUI();
				groups.get(i).outer.updateUI();
				groups.get(i).inner.updateUI();
				groups.get(i).label.updateUI();
			}
		}
	}
	
	public void removeGroup(Group group) {
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).group == group) {
				scrollPanel.remove(groups.get(i).outer);
				groups.remove(i);
			
				if(i == 0)
					i = 1;
				
				if (groups.size() != 0)
					markeGroup(i-1);
				break;
			}

			if (i == (groups.size() - 1))
				System.out.println("group not found");
		}

		scrollPanel.updateUI();
	}

	private void markeGroup(MouseEvent e) {
		int index = 0;
		for (int i = 0; i < groups.size(); i++) {
			if (e.getSource() == groups.get(i).label
					|| e.getSource() == groups.get(i).inner)
				index = i;
			else {

				groups.get(i).inner.setBackground(Color.white);
				groups.get(i).label.setForeground(Color.black);
				groups.get(i).marked = false;
			}
		}

		groups.get(index).inner.setBackground(Color.blue);
		groups.get(index).label.setForeground(Color.white);
		groups.get(index).marked = true;
		
		try {
			Thread.sleep(300);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if(e.getClickCount() == 2){
			groups.get(index).group.setOpen(true);
			mf.addGroup(groups.get(index).group);
		}
	}

	private void markeGroup(int index) {
		for (int i = 0; i < groups.size(); i++) {
			if (i != index) {
				groups.get(i).inner.setBackground(Color.white);
				groups.get(i).label.setForeground(Color.black);
				groups.get(i).marked = false;

			}
		}

		groups.get(index).inner.setBackground(Color.blue);
		groups.get(index).label.setForeground(Color.white);
		groups.get(index).marked = true;
	}

	private JScrollPane initComponents() {

		JScrollPane scrollPane = new JScrollPane();
		JButton newGroup = new JButton();
		JButton leaveGroup = new JButton();

		setPreferredSize(new java.awt.Dimension(230, 400));
		setVerifyInputWhenFocusTarget(false);
		
		scrollPanel.setBackground(Color.WHITE);

		newGroup.setText("New Group");
		newGroup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				newGroupActionPerformed(evt);
			}
		});

		leaveGroup.setText("Leave Group");
		leaveGroup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				leaveGroupActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(scrollPane)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(newGroup,
										GroupLayout.PREFERRED_SIZE, 103,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(leaveGroup,
										GroupLayout.PREFERRED_SIZE, 103,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(12, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
								312, Short.MAX_VALUE)
						.addGap(33, 33, 33)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(newGroup)
										.addComponent(leaveGroup))
						.addGap(26, 26, 26)));

		return scrollPane;
	}

	private void newGroupActionPerformed(ActionEvent evt) {
		/**
		 * Mer programmkod fylls i ett senare skede
		 */
		
//		addGroup(new Group("kik", 1));
		new CreateGroupFrame(mf);
			}

	private void leaveGroupActionPerformed(ActionEvent evt) {	
		Group removeThisGroup = null;
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).marked) {
				removeThisGroup = groups.get(i).group;
				break;
			}
		}
		
		/**
		 * Mer programmkod fylls i ett senare skede
		 */	
		
		removeGroup(removeThisGroup);
	}

	private class GroupBox {
		JPanel inner;
		JPanel outer;
		JLabel label;
		Group group;
		boolean marked = false;

		public GroupBox(JPanel outer, JPanel inner, JLabel label, Group group) {
			super();
			this.inner = inner;
			this.outer = outer;
			this.label = label;
			this.group = group;
		}
	}

	public class ListenerPanel implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {

		}

		@Override
		public void mousePressed(MouseEvent e) {
			markeGroup(e);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub

		}

	}


//	public static void main(String[] args) {
//		//Testkod
//		
//		JFrame frame = new JFrame("Name");
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setVisible(true);
//		frame.setPreferredSize(new Dimension(400, 400));
//		frame.setLocation(400, 200);
//		frame.setBackground(Color.white);
//		
//		
//		frame.add(new GroupPanel(new MainFrame());
//		
//		frame.pack();
//		
//	}

}
